package dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeListener;

import panels.CosysmoverviewPanel;

import main.GlobalMethods;
import main.Icons;

import core.COINCOMOComponent;
import core.COINCOMOSubSystem;
import database.COINCOMOSubSystemManager;

public class CotGlueCodeEAFPanel extends JPanel implements ActionListener {
	
	DecimalFormat format1Decimal = new DecimalFormat( "0.0" );
    DecimalFormat format2Decimals = new DecimalFormat( "0.00" );
    //Arnab
    private JLabel[] personnelDriverLabels = new JLabel[]
    {
        new JLabel( "ACIEP" ) , new JLabel( "ACIPC" ) , new JLabel( "AXCIP" ) , new JLabel( "APCON" )
    };

    private JLabel[] cotsComponentDriverLabels = new JLabel[]
    {
        new JLabel( "ACPMT" ) , new JLabel( "ACSEW" ) , new JLabel( "ACPPS" ) , new JLabel( "ACPTD" )
    };

    private JLabel[] appSysDriverLabels = new JLabel[]
    {
        new JLabel( "ACREL" ) , new JLabel( "ACPER" ), new JLabel( "ASPRT" )
    };
    
    private JLabel[] apcpxDriverLabels = new JLabel[]
    {
       new JLabel( "<html><body style='text-align:center'>Interface<br />Convention<br /></body></html>" ) , new JLabel( "<html><body style='text-align:center'>Control<br />Aspects<br /></body></html>" ) , new JLabel( "Data" )
    };
    
    private JLabel[] aacpxDriverLabels = new JLabel[]
    {
       new JLabel( "<html><body style='text-align:center'>Interface<br />Convention<br /></body></html>" ) , new JLabel( "<html><body style='text-align:center'>Control<br />Aspects<br /></body></html>" ) , new JLabel( "Data" )
    };
   
     private JButton[] personnelDriverButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };

     private JButton[] cotsComponentDriverButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) 
    };

    private JButton[] appSysDriverButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };

    private JButton[] apcpxDriverButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };
  
    private JButton[] aacpxDriverButtons = new JButton[]
    {
       new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };
    
     private JLabel[] driverLabels = new JLabel[]
    {
        new JLabel( "Integration Personal Drivers" ) , new JLabel( "COTS/NDI Components" ) , new JLabel( "Application System Drivers" ), new JLabel( "APCPX" ), new JLabel( "AACPX" )
    };

    private JLabel[][] driverSetLabels = new JLabel[][]
    {
        personnelDriverLabels ,
        cotsComponentDriverLabels ,
        appSysDriverLabels ,
        apcpxDriverLabels , 
        aacpxDriverLabels , 
    };

    private JButton[][] ratingSetButtons = new JButton[][]
    {
    	personnelDriverButtons ,
        cotsComponentDriverButtons ,
        appSysDriverButtons ,
        apcpxDriverButtons ,
        aacpxDriverButtons , 
    };

    
    JPopupMenu ratingPopup = new JPopupMenu();

    JPopupMenu incrementPopup = new JPopupMenu();

    JMenuItem vloMenuItem = new JMenuItem( "VLO" );

    JMenuItem loMenuItem = new JMenuItem( "LO" );

    JMenuItem nomMenuItem = new JMenuItem( "NOM" );

    JMenuItem hiMenuItem = new JMenuItem( "HI" );

    JMenuItem vhiMenuItem = new JMenuItem( "VHI" );

    JButton applyButton = new JButton( "Apply" );

    JButton resetButton = new JButton( "Reset" );

    JButton closeButton = new JButton( "Close" );
    // The Button From Which the Popup Generated ...
    JButton ownerButton = null;

    COINCOMOComponent component = null;

    // For Updating the Table
    int id = 0 ;

    /**
     *
     * @param frame to be used to set the Owner of the Dialog
     * @param subComponent object to read from and save to
     * @param rowNumber to update the corresponding CLEF's SubComponent
     */
    
    public CotGlueCodeEAFPanel( JFrame frame , COINCOMOComponent subSystem2 , int id )
    {

        this.id = id ;
        this.component = subSystem2;


        GlobalMethods.updateStatusBar( "Done." );
        
     // Popup
        ratingPopup.add( vloMenuItem );
        ratingPopup.add( loMenuItem );
        ratingPopup.add( nomMenuItem );
        ratingPopup.add( hiMenuItem );
        ratingPopup.add( vhiMenuItem );


        // Menu Items
        vloMenuItem.addActionListener( this );
        loMenuItem.addActionListener( this );
        nomMenuItem.addActionListener( this );
        hiMenuItem.addActionListener( this );
        vhiMenuItem.addActionListener( this );

       
        this.setLayout( null );

        int ratings[] = subSystem2.getRatings() ;
        int counter = 0 ;
        
        if(subSystem2.getLoadedCocotsRatings().containsKey(new Integer(id)))
        	loadParameters();

        // GUI
        for ( int i = 0 ; i < driverSetLabels.length ; i++ )
        {
            // Legend
            TitledBorder legendTitleBorder = BorderFactory.createTitledBorder( driverLabels[i].getText() );
            legendTitleBorder.setTitleColor( Color.BLUE );

            this.add( driverLabels[i] );

            driverLabels[i].setText( "" );
            driverLabels[i].setBorder( legendTitleBorder );
            driverLabels[i].setFont( new Font( "courier" , 1 , 12 ) );

           
            driverLabels[i].setBounds( 10 , 10 + i * 100 , 530 , 100 );
            

            JLabel label1 = new JLabel( "Rating" );
           // JLabel label2 = new JLabel( "% Incr" );

            label1.setFont( new Font( "arial" , 1 , 11 ) );
          //  label2.setFont( new Font( "arial" , 1 , 11 ) );

            this.add( label1 );
          //  this.add( label2 );

           
                label1.setBounds( 40 , 10 + i * 100 + 35 , 65 , 25 );
           //     label2.setBounds( 40 , 10 + i * 100 + 65 , 65 , 25 );
            

            for ( int j = 0 ; j < driverSetLabels[i].length ; j++ )
            {
                this.add( driverSetLabels[i][j] );
                this.add( ratingSetButtons[i][j] );
                

                ratingSetButtons[i][j].setName( driverSetLabels[i][j].getText() );
                
                if(!subSystem2.getLoadedCocotsRatings().containsKey(new Integer(id)))
                	ratingSetButtons[i][j].setText( GlobalMethods.convertToRating( ratings[counter] ) ) ;
                //incrementSetButtons[i][j].setText( GlobalMethods.convertToPercent( percent[counter++] ) ) ;

                ratingSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );
                //incrementSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );

                ratingSetButtons[i][j].setFocusable( false );
                //incrementSetButtons[i][j].setFocusable( false );

                ratingSetButtons[i][j].addActionListener( this );
                //incrementSetButtons[i][j].addActionListener( this );

                driverSetLabels[i][j].setHorizontalAlignment( JLabel.CENTER );
                driverSetLabels[i][j].setFont( new Font( "arial" , 1 , 11 ) );

               
                    driverSetLabels[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 10 , 65 , 25 );
                    ratingSetButtons[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 35 , 65 , 25 );
                    //incrementSetButtons[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 65 , 65 , 25 );
                
            }
        }
        
        
        
        this.setSize( 570 , 550);
        this.setVisible( true );

    }

    public void loadParameters()
    {
    	HashMap<Integer, int[]> loadedParameters = component.getLoadedCocotsRatings();
    	if( loadedParameters.size() > 0 )
    	{
	    	int[] ratings = loadedParameters.get(id);
	    	int counter = 0;
	    	
	    	for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
	        {
	            for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
	            {
	                ((JButton) ratingSetButtons[i][j]).setText( GlobalMethods.convertIntegerToRating(ratings[counter++]) );
	                //percent[counter++] = GlobalMethods.convertFromPercent( ((JButton) incrementSetButtons[i][j]).getText() );
	            }
	        }
    	}
    }
    
	@Override
	public void actionPerformed( ActionEvent e ) {
		// TODO Auto-generated method stub
		
		if ( e.getSource() == vloMenuItem || e.getSource() == loMenuItem || e.getSource() == nomMenuItem ||
                e.getSource() == hiMenuItem || e.getSource() == vhiMenuItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        else
        {
            ownerButton = ((JButton) e.getSource());
            // Default
            vloMenuItem.setEnabled( true );
            loMenuItem.setEnabled( true );
            nomMenuItem.setEnabled( true );
            hiMenuItem.setEnabled( true );
            vhiMenuItem.setEnabled( true );
            
            
            if( ownerButton.getName().equals( "ACIEP" ) )
            {
                
            }
            else if( ownerButton.getName().equals( "ACIPC" ) )
            {
                //vloMenuItem.setEnabled( false );
                
            }
            else if( ownerButton.getName().equals( "AXCIP" ) )
            {
            	vloMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "APCON" ) )
            {
            }
            else if( ownerButton.getName().equals( "ACPMT" ) )
            {
                
            }
            else if( ownerButton.getName().equals( "ACSEW" ) )
            {
            	vloMenuItem.setEnabled( false );
            	
            }
            else if( ownerButton.getName().equals( "ACPPS" ) )
            {
            	vloMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "ACPTD" ) )
            {
                
            }
            else if( ownerButton.getName().equals( "ACREL" ) )
            {
            	vloMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "ACPER" ) )
            {
            	vloMenuItem.setEnabled(false);
            	loMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "ASPRT" ) )
            {
                vloMenuItem.setEnabled(false);
            	loMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "IC" ) )
            {
            	vloMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "CA" ) )
            {
            	vloMenuItem.setEnabled(false);
                
            }
            else if( ownerButton.getName().equals( "Data" ) )
            {
                
            }
            

            // Determine Which Popup to show ...
            if ( ownerButton.getText().endsWith( "%" ) )
            {
                incrementPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
            else
            {
                ratingPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
        }
		
	}
	
	
	public void getRatings( final Integer cot_id)
	{
		int ratings[] = new int[17];
		//int percent[] = new int[ 18 ];

		int counter = 0 ;

		for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
		{
			for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
			{
				//ratings[counter++] = ((JButton) ratingSetButtons[i][j]).getText();
				ratings[counter++] = (int) GlobalMethods.convertRatingToInteger( (String)((JButton) ratingSetButtons[i][j]).getText() );
				//percent[counter++] = GlobalMethods.convertFromPercent( ((JButton) incrementSetButtons[i][j]).getText() );
			}
		}
		component.setCocotsRatings( cot_id , ratings);
		
	}
}
